/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ohos.ringdroid.core;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.multimodalinput.event.KeyEvent;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Represents a draggable start or end marker.
 * <p>
 * Most events are passed back to the client class using a
 * listener interface.
 * <p>
 * This class directly keeps track of its own velocity, though,
 * accelerating as the user holds down the left or right arrows
 * while this control is focused.
 */
public class MarkerView extends Component implements Component.TouchEventListener, Component.KeyEventListener {

    public interface MarkerListener {
        public void markerTouchStart(MarkerView marker, float pos);

        public void markerTouchMove(MarkerView marker, float pos);

        public void markerTouchEnd(MarkerView marker);

        public void markerFocus(MarkerView marker);

        public void markerLeft(MarkerView marker, int velocity);

        public void markerRight(MarkerView marker, int velocity);

        public void markerEnter(MarkerView marker);

        public void markerKeyUp();

        public void markerDraw();
    }

    private int mVelocity;
    private MarkerListener mListener;

    public MarkerView(Context context, AttrSet attrs) {
        super(context, attrs);

        // Make sure we get keys
        setFocusable(0);

        mVelocity = 0;
        mListener = null;


    }

    public void setListener(MarkerListener listener) {
        mListener = listener;
    }


    @Override
    public void setTouchEventListener(TouchEventListener listener) {
        super.setTouchEventListener(listener);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                requestFocus();
                // We use raw x because this window itself is going to
                // move, which will screw up the "local" coordinates
                mListener.markerTouchStart(this, (touchEvent.getIndex()));
                break;
            case TouchEvent.POINT_MOVE:
                // We use raw x because this window itself is going to
                // move, which will screw up the "local" coordinates
                mListener.markerTouchStart(this, (touchEvent.getIndex()));
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                mListener.markerTouchEnd(this);
                break;
        }
        return true;
    }


    @Override
    public void setFocusChangedListener(FocusChangedListener listener) {
        mListener.markerFocus(this);
        super.setFocusChangedListener(listener);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        if (mListener != null)
            mListener.markerDraw();
    }


    @Override
    public boolean onKeyEvent(Component component, KeyEvent keyEvent) {
        return false;
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        mVelocity++;
//        int v = (int) Math.sqrt(1 + mVelocity / 2);
//        if (mListener != null) {
//            if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
//                mListener.markerLeft(this, v);
//                return true;
//            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
//                mListener.markerRight(this, v);
//                return true;
//            } else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
//                mListener.markerEnter(this);
//                return true;
//            }
//        }
//
//        return super.onKeyDown(keyCode, event);
//    }

//    @Override
//    public boolean onKeyUp(int keyCode, KeyEvent event) {
//        mVelocity = 0;
//        if (mListener != null)
//            mListener.markerKeyUp();
//        return super.onKeyDown(keyCode, event);
//    }

}
